<template>
  <!-- 批次打印 -->
  <div id="labbatchprinted">
    <Breadcrumb content="批次打印" v-if="showBreadCrumbTag === true"></Breadcrumb>
    <GoBack :content="'批次打印'" v-if="showBreadCrumbTag === false" @closeDrawer="closeDrawer" :backUrl="url"></GoBack>
    <div class="bodyContent">
      <Splitpanes>
        <Pane size="20">
          <el-scrollbar style="height: 100%">
            <vxe-form ref="searchConForm" :data="form" title-width="80" class="padding-sm width-100-percentage topVxeForm">
              <vxe-form-item title="报告单元" class-name="width-100-percentage">
                <template #default>
                  <SelectWorkGroupAll ref="selectWorkGroupRef" :url="'/api/rptGroup/findRptGroup'" :inputWidth="'100%'" @fatherMethod="searchWklt" :isShowAll="true" :isClear="true"></SelectWorkGroupAll>
                </template>
              </vxe-form-item>
              <vxe-form-item title="标本号" class-name="width-100-percentage">
                <template #default="{ data }">
                  <vxe-input class="enterNextInputFlag" type="text" v-model="data.sampleNo" placeholder="请输入1,2,5-8等格式" clearable class-name="width-100-percentage" @blur="searchWaitPrintList"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="标本日期[起]" class-name="width-100-percentage">
                <template #default="{ data }">
                  <el-date-picker v-model="data.scheduleDateBegin" :value-format="'YYYY-MM-DD HH:mm:ss'" :popper-style="{ zIndex: 9999999 }" :tabindex="999000" type="datetime" placeholder="标本日期[起]" :shortcuts="shortcuts" style="width: 100%" />
                </template>
              </vxe-form-item>
              <vxe-form-item title="标本日期[止]" class-name="width-100-percentage">
                <template #default="{ data }">
                  <el-date-picker v-model="data.scheduleDateEnd" :value-format="'YYYY-MM-DD HH:mm:ss'" :popper-style="{ zIndex: 9999999 }" :tabindex="999000" type="datetime" placeholder="标本日期[止]" :shortcuts="shortcutsEnd" style="width: 100%" />
                </template>
              </vxe-form-item>
              <vxe-form-item title="病人类型" class-name="width-100-percentage">
                <template #default="{ data }">
                  <VxePulldownMultiSelection childIndex="visitType" url="/api/sys/basDictDetail/findDictDetailList" :dataObj="{ code: 'PATTYPE' }" @confirmClick="(data) => confirmList(data, 'visitType')" :tableColumn="[
                    { field: 'name', title: '名称' },
                    { field: 'code', title: '编号' }
                  ]" :displayTexts="['name']" :displayTextObj="data.visitTypeName" :dicValues="data.visitTypes" includeVal="commonId" />
                </template>
              </vxe-form-item>
              <vxe-form-item title="病区" class-name="width-100-percentage">
                <template #default="{ data }">
                  <VxePulldownMultiSelection childIndex="reqWard" url="/api/sys/findDepartmentList" :dataObj="{ orgId: store.userInfo.orgId }" @confirmClick="(data) => confirmList(data, 'reqWard')" :tableColumn="[
                    { field: 'code', title: '编号' },
                    { field: 'name', title: '名称' }
                  ]" :displayTexts="['name']" :displayTextObj="data.reqWardName" :dicValues="data.reqWards" includeVal="id" />
                </template>
              </vxe-form-item>
              <vxe-form-item title="科室" class-name="width-100-percentage">
                <template #default="{ data }">
                  <VxePulldownMultiSelection childIndex="dep" url="/api/sys/findDepartmentList" :dataObj="{ orgId: store.userInfo.orgId }" @confirmClick="(data) => confirmList(data, 'dep')" :tableColumn="[
                    { field: 'code', title: '编号' },
                    { field: 'name', title: '名称' }
                  ]" :displayTexts="['name']" :displayTextObj="data.depName" :dicValues="data.deps" includeVal="id" />
                </template>
              </vxe-form-item>

              <vxe-form-item field="registrantName" title="检验医师" class-name="width-100-percentage">
                <template #default="{ data }">
                  <VxePulldownMultiSelection childIndex="registrant" :url="'api/user/getUserInfoList?skipCount=0&maxResultCount=200'" @confirmClick="(data) => confirmList(data, 'registrant')" :tableColumn="[
                    { field: 'username', title: '编号' },
                    { field: 'realname', title: '名称' }
                  ]" :displayTexts="['realname']" :displayTextObj="data.registrantName" :dicValues="data.registrants" includeVal="id" />
                </template>
              </vxe-form-item>
              <vxe-form-item title="审核标志" class-name="width-100-percentage">
                <vxe-radio-group v-model="form.isReviewed" @change="searchWaitPrintList">
                  <vxe-radio-button :label="null">全部</vxe-radio-button>
                  <vxe-radio-button :label="1">已审核</vxe-radio-button>
                  <vxe-radio-button :label="0">未审核</vxe-radio-button>
                </vxe-radio-group>
              </vxe-form-item>
              <vxe-form-item title="打印标志" class-name="width-100-percentage">
                <vxe-radio-group v-model="form.isPrinted" @change="searchWaitPrintList">
                  <vxe-radio-button :label="null">全部</vxe-radio-button>
                  <vxe-radio-button :label="1">已打印</vxe-radio-button>
                  <vxe-radio-button :label="0">未打印</vxe-radio-button>
                </vxe-radio-group>
              </vxe-form-item>
              <vxe-form-item title="筛选条件" class-name="width-100-percentage">
                <vxe-checkbox v-model="form.isFilterNoResult" @change="searchWaitPrintList" border>无检验结果不打印</vxe-checkbox>
                <br />
                <vxe-checkbox v-model="form.isFilterNoPatName" @change="searchWaitPrintList" border>无病人姓名不打印</vxe-checkbox>
              </vxe-form-item>
            </vxe-form>
            <el-divider content-position="left">打印顺序</el-divider>
            <vxe-form class="printSeq" :model="form">
              <vxe-form-item>
                <vxe-radio-group v-model="form.customSorting" @change="searchWaitPrintList">
                  <vxe-radio label="0" style="margin-right: 60px">标本号</vxe-radio>
                  <vxe-radio label="4">科室</vxe-radio>
                  <vxe-radio label="1">科室 + 床号 + 标本号</vxe-radio>
                  <vxe-radio label="2">病人类型 + 科室 + 床号 + 标本号</vxe-radio>
                  <vxe-radio label="3">病人类型 + 科室 + 标本号</vxe-radio>
                </vxe-radio-group>
              </vxe-form-item>
            </vxe-form>
          </el-scrollbar>
        </Pane>
        <Pane size="80">
          <WkltResult ref="testListWaitPrintRef" :triggerSource="'2'" @search="searchWaitPrintList" />
        </Pane>
      </Splitpanes>
    </div>

  </div>
</template>

<script lang="ts" setup>
import Breadcrumb from '../../../components/Breadcrumb.vue';//面包屑
import { Splitpanes, Pane } from 'splitpanes';
import GoBack from '@/components/GoBack.vue'; //返回组件
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common';
import WkltResult from '@/views/lab_sys/lab_set/WkltResult.vue'; //工作列表及结果
import { GetRptGroupOptions } from '@/utils/SysOptionsHelper';
import VxePulldownMultiSelection from '@/components/VxePulldownMultiSelection.vue';
import SelectWorkGroupAll from '../lab_bus/components/SelectWorkGroupAll.vue'; //报告单元默认全部引入下拉组件


import { AppStore } from '@/stores/store';
let store = AppStore();


// 全局变量定义区 ==========
defineExpose({
  init
})

let { proxy }: any = getCurrentInstance();
let showBreadCrumbTag = ref(true)//当为true时显示的是普通面包屑 false显示的是返回组件
let url = ref('');//抽屉要返回的路由地址
const orgList = store.userInfo.orgList;
let defaultProps = {
  children: 'children',
  label: 'name'
};
//病人类型
const visitTypeList: any[] = reactive([]);
const specTypeDict: any[] = reactive([]);
let testListWaitPrintRef = ref(null);
let form: any = reactive({
  id: '', //当前选中工作列表结果id
  /*  orgId: store.userInfo.orgId,
    orgName: store.userInfo.orgName,*/
  orgId: '',
  orgName: '',
  wtId: '', //选中的工作组
  scheduleDateBegin: formatDateTime(new Date().valueOf() - 0 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd 00:00:00'),
  scheduleDateEnd: formatDateTime(new Date().valueOf(), 'yyyy-MM-dd 23:59:59'),
  sampleNo: '',
  visitTypeName: '',
  visitTypes: [], //病人类型
  specTypeName: [], //样本类型
  specTypeIds: [], //样本类型
  isReviewed: null, //是否审核
  isPrinted: 0, //是否打印
  isFilterNoResult: true, //根据结果筛选
  isFilterNoPatName: false, //根据病人姓名筛选
  isFilterNoSpecCode: false,//是否过滤无标本号数据
  processItem: [], //筛选条件的处理项
  customSorting: '0', //打印顺序
  deps: [], //科室
  depName: '',
  reqWardName: '',
  reqWards: [],
  testDoctor: '',
  registrantName: '',
  registrants: []
});
const shortcuts = [
  {
    text: '今天',
    value: formatDateTime(new Date(), 'yyyy-MM-dd 00:00:00')
  },
  {
    text: '昨天',
    value: () => {
      const date = new Date();
      date.setDate(date.getDate() - 1);
      return formatDateTime(date, 'yyyy-MM-dd 00:00:00');
    }
  },
  {
    text: '一周前',
    value: () => {
      const date = new Date();
      date.setDate(date.getDate() - 7);
      return formatDateTime(date, 'yyyy-MM-dd 00:00:00');
    }
  }
];

const shortcutsEnd = [
  {
    text: '今天',
    value: formatDateTime(new Date(), 'yyyy-MM-dd 23:59:59')
  },
  {
    text: '昨天',
    value: () => {
      const date = new Date();
      date.setDate(date.getDate() - 1);
      return formatDateTime(date, 'yyyy-MM-dd 23:59:59');
    }
  },
  {
    text: '一周前',
    value: () => {
      const date = new Date();
      date.setDate(date.getDate() - 7);
      return formatDateTime(date, 'yyyy-MM-dd 23:59:59');
    }
  }
];

onMounted(() => {
  getSampleType();
});
/**下拉选中 */
function dropSelectConfirm(e) {
  e.aimKeys.forEach((m) => {
    form[m.aimKey] = e.row[m.rowKey];
  });
  searchWaitPrintList();
}

//获取工作列表
function searchWklt(selectedRadioId, selectedName) {
  form.wtId = selectedRadioId.value;
  searchWaitPrintList();
}

function handleOrgNodeClick(data: any) {
  form.orgId = data.id;
  form.orgName = data.name;
  searchWaitPrintList();
  const selectOrgRef: any = proxy.$refs['selectOrgRef'];
  selectOrgRef.blur();
}

function init(row: any, row2: any) {
  url.value = row
  showBreadCrumbTag.value = !row2
}

// 接收子组件传过来的选中的id(选择的那个工作组的id)
function getSelectedId(propValue: any) {
  GetRptGroupOptions(propValue.value);
  form.wtId = propValue.value;
  searchWaitPrintList();
}

// 关闭抽屉
function closeDrawer() {
  showBreadCrumbTag.value = true //恢复成面包屑
  proxy.$emit('closeDrawer')
}

/**查询检验主记录 */
function searchWaitPrintList() {
  if (testListWaitPrintRef.value) {
    testListWaitPrintRef.value?.getWklt(form);
  }
}

// 多选框绑定值变化
function checkedChange(items, name, isChange) {
  form[name] = items;
  processItemChange(isChange);
}

// 筛选条件过滤
function processItemChange(isChange) {
  if (!isChange) {
    return;
  }
  if (form.processItem.length > 0) {
    form.processItem.forEach((item) => {
      form[item] = true;
    });
  } else {
    form.isFilterNoPatName = false;
    form.isFilterNoResult = false;
    form.isFilterNoSpecCode = false;
  }
  //selectWtIdChange();
}

//获取样本类型
function getSampleType() {
  specTypeDict.length = 0;
  const url = '/api/lab/labSampleType/findSampleTypeList';
  proxy.$http
    .get(url)
    .then((res: any) => {
      Object.assign(specTypeDict, res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
function confirmList(data, keyName) {
  form[keyName + 's'] = [];
  form[keyName + 'Ids'] = [];
  form[keyName + 'Name'] = '';
  if (IsEmpty(data.aimKeys) || data.aimKeys.length === 0) {
    const arr = data.arr;
    form[keyName + 's'] = arr;
    if (arr.length !== 0) {
      arr.forEach((item) => {
        form[keyName + 'Ids'].push(item.id);
        if (item.name) {
          form[keyName + 'Name'] += item.name + ',';
        } else if (item[keyName + 'Name']) {
          form[keyName + 'Name'] += item[keyName + 'Name'] + ',';
        } else {
          form[keyName + 'Name'] += item.realname + ',';
        }
      });

      if (form[keyName + 'Name'] && form[keyName + 'Name'].trim()) {
        form[keyName + 'Name'] = form[keyName + 'Name'].slice(0, -1);
      }
    }
  } else {
    data.arr.forEach((item) => {
      data.aimKeys.forEach((val) => {
        form[val.targetField + 's'].push(item);
        form[val.targetField + 'Ids'].push(item[val.targetField + 'Id']);
        form[val.targetField + 'Name'] += item[val.targetField].name + ',';
      });
    });
  }
}

</script>
<style lang="scss" scoped>
#labbatchprinted {
  height: 100%;

  .bodyContent {
    height: calc(100% - 60px);

    .splitpanes {
      background-color: $base-white;

      .topVxeForm {
        .vxe-checkbox {
          margin: 8px 10px 0 0;
        }
      }

      .el-divider--horizontal {
        margin: 15px 0;
      }

      :deep(.printSeq) {
        .vxe-form--item-content {
          margin-left: 30px;
        }

        .vxe-radio {
          margin: 10px;
          display: block;
        }
      }
    }
  }
}
</style>